package com.ruoyi.functiontest.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.functiontest.mapper.TollMileageStatTestMapper;
import com.ruoyi.functiontest.domain.TollMileageStatTest;
import com.ruoyi.functiontest.service.ITollMileageStatTestService;

/**
 * 收费里程实测数据测试Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-24
 */
@Service
public class TollMileageStatTestServiceImpl implements ITollMileageStatTestService 
{
    @Autowired
    private TollMileageStatTestMapper tollMileageStatTestMapper;

    /**
     * 查询收费里程实测数据测试
     * 
     * @param id 收费里程实测数据测试主键
     * @return 收费里程实测数据测试
     */
    @Override
    public TollMileageStatTest selectTollMileageStatTestById(Long id)
    {
        return tollMileageStatTestMapper.selectTollMileageStatTestById(id);
    }

    /**
     * 查询收费里程实测数据测试列表
     * 
     * @param tollMileageStatTest 收费里程实测数据测试
     * @return 收费里程实测数据测试
     */
    @Override
    public List<TollMileageStatTest> selectTollMileageStatTestList(TollMileageStatTest tollMileageStatTest)
    {
        return tollMileageStatTestMapper.selectTollMileageStatTestList(tollMileageStatTest);
    }

    /**
     * 新增收费里程实测数据测试
     * 
     * @param tollMileageStatTest 收费里程实测数据测试
     * @return 结果
     */
    @Override
    public int insertTollMileageStatTest(TollMileageStatTest tollMileageStatTest)
    {
        return tollMileageStatTestMapper.insertTollMileageStatTest(tollMileageStatTest);
    }

    /**
     * 修改收费里程实测数据测试
     * 
     * @param tollMileageStatTest 收费里程实测数据测试
     * @return 结果
     */
    @Override
    public int updateTollMileageStatTest(TollMileageStatTest tollMileageStatTest)
    {
        return tollMileageStatTestMapper.updateTollMileageStatTest(tollMileageStatTest);
    }

    /**
     * 批量删除收费里程实测数据测试
     * 
     * @param ids 需要删除的收费里程实测数据测试主键
     * @return 结果
     */
    @Override
    public int deleteTollMileageStatTestByIds(Long[] ids)
    {
        return tollMileageStatTestMapper.deleteTollMileageStatTestByIds(ids);
    }

    /**
     * 删除收费里程实测数据测试信息
     * 
     * @param id 收费里程实测数据测试主键
     * @return 结果
     */
    @Override
    public int deleteTollMileageStatTestById(Long id)
    {
        return tollMileageStatTestMapper.deleteTollMileageStatTestById(id);
    }

    @Override
    public boolean hasChildById(Long id)
    {
        return tollMileageStatTestMapper.hasChildById(id) > 0;
    }

    /**
     * 批量更新实测单位审核状态（review1字段设置为0）
     * 
     * @return 更新的记录数
     */
    @Override
    public int updateReview1ToZero()
    {
        return tollMileageStatTestMapper.updateReview1ToZero();
    }

    /**
     * 校验所有数据的review1字段是否都为0
     * 
     * @return true 所有review1都为0，false 存在review1不为0的数据
     */
    @Override
    public boolean checkAllReview1IsZero()
    {
        int count = tollMileageStatTestMapper.countReview1NotZero();
        return count == 0;
    }

    /**
     * 根据条件批量更新业主审核状态（review2字段设置为0）
     * 
     * @param tollMileageStatTest 查询条件
     * @return 更新的记录数
     */
    @Override
    public int updateReview2ToZeroByCondition(TollMileageStatTest tollMileageStatTest)
    {
        return tollMileageStatTestMapper.updateReview2ToZeroByCondition(tollMileageStatTest);
    }

    /**
     * 校验所有数据的review1和review2字段是否都不为空且都为0
     * 
     * @return true 所有review1和review2都不为空且为0，false 存在不符合条件的数据
     */
    @Override
    public boolean checkAllReview1AndReview2AreZero()
    {
        int count = tollMileageStatTestMapper.countReview1OrReview2NotZero();
        return count == 0;
    }

    /**
     * 批量更新中心审核状态（review3字段设置为0）
     * 
     * @return 更新的记录数
     */
    @Override
    public int updateReview3ToZero()
    {
        return tollMileageStatTestMapper.updateReview3ToZero();
    }

    /**
     * 导入收费里程实测数据测试数据
     * 简化导入逻辑：直接按照Excel原始格式导入，通过id和parentid来确定数据
     * 
     * @param tollMileageStatTestList 收费里程实测数据测试列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @return 结果
     */
    @Override
    public String importTollMileageStatTest(List<TollMileageStatTest> tollMileageStatTestList, Boolean isUpdateSupport)
    {
        if (tollMileageStatTestList == null || tollMileageStatTestList.isEmpty())
        {
            return "导入数据不能为空！";
        }
        
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        
        // 简化导入逻辑：直接按照Excel原始格式导入
        for (TollMileageStatTest data : tollMileageStatTestList)
        {
            try
            {
                // 基本数据验证
                if (data.getRoadName() == null) {
                    data.setRoadName("");
                }
                
                // 通过id和parentid来确定数据
                TollMileageStatTest existingNode = null;
                
                // 使用新的方法通过id和parentid组合来精确匹配数据
                TollMileageStatTest query = new TollMileageStatTest();
                query.setId(data.getId());
                query.setParentId(data.getParentId());
                
                List<TollMileageStatTest> existingNodes = tollMileageStatTestMapper.selectTollMileageStatTestByIdAndParentId(query);
                if (existingNodes != null && !existingNodes.isEmpty()) {
                    existingNode = existingNodes.get(0);
                }
                
                if (existingNode != null) {
                    // 已存在节点，如果支持更新则更新
                    if (isUpdateSupport) {
                        existingNode.setRoadName(data.getRoadName());
                        existingNode.setNodeName(data.getNodeName());
                        existingNode.setLeftLength(data.getLeftLength());
                        existingNode.setRightLength(data.getRightLength());
                        existingNode.setAvgLength(data.getAvgLength());
                        existingNode.setRemark(data.getRemark());
                        existingNode.setParentId(data.getParentId());
                        
                        tollMileageStatTestMapper.updateTollMileageStatTest(existingNode);
                        successNum++;
                    } else {
                        // 不支持更新，跳过此条数据
                        failureNum++;
                        failureMsg.append("<br/>" + failureNum + ". 数据已存在，跳过: " + data.getRoadName() + "-" + data.getNodeName());
                    }
                } else {
                    // 不存在节点，创建新节点
                    TollMileageStatTest newNode = new TollMileageStatTest();
                    newNode.setId(data.getId()); // 使用Excel中的id值
                    newNode.setRoadName(data.getRoadName());
                    newNode.setNodeName(data.getNodeName());
                    newNode.setLeftLength(data.getLeftLength());
                    newNode.setRightLength(data.getRightLength());
                    newNode.setAvgLength(data.getAvgLength());
                    newNode.setRemark(data.getRemark());
                    newNode.setParentId(data.getParentId());
                    
                    tollMileageStatTestMapper.insertTollMileageStatTest(newNode);
                    successNum++;
                }
                
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + ". 导入失败：";
                if (data.getRoadName() != null)
                {
                    msg += "路段名称：" + data.getRoadName();
                }
                if (data.getNodeName() != null)
                {
                    msg += "，起点/迄点：" + data.getNodeName();
                }
                failureMsg.append(msg + e.getMessage());
            }
        }
        
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            return failureMsg.toString();
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据已导入到系统。");
            return successMsg.toString();
        }
    }
}
